package com.tsd.jxc.req;

import com.alibaba.fastjson.JSON;
import com.tsd.core.datasource.SpringContextUtil;
import com.tsd.core.utils.DoubleUtil;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.ListUtil;
import com.tsd.jxc.goods.dao.JxcGoodsMapper;
import com.tsd.jxc.goods.vo.JxcGoodsVo;
import com.tsd.jxc.setting.JxcSettingUtil;
import com.tsd.jxc.stock.entity.JxcStockInbill;
import com.tsd.jxc.stock.entity.JxcStockInbillExt;
import com.tsd.jxc.stock.entity.JxcStockInbillItemExt;
import com.tsd.jxc.utils.ClothingUtil;
import com.tsd.jxc.vo.JxcSettingVo;
import com.tsd.utils.AutoInjectUtils;

import java.math.BigDecimal;
import java.util.*;

/**
 * @ClassName: JxcStockInBillReqParamsVo
 * @Description: TODO
 * @Author: Hillpool
 * @Date: 2023/3/15 15:53
 * @Version: 1.0
 */
public class JxcStockInBillReqParamsVo extends JxcStockInbill {

    private List<JxcStockInBillItemReqParamsVo> item_list;

    public List<JxcStockInBillItemReqParamsVo> getItem_list() {
        return item_list;
    }

    public void setItem_list(List<JxcStockInBillItemReqParamsVo> item_list) {
        this.item_list = item_list;
    }

    public JxcStockInbillExt convert() {
        JxcStockInbillExt inbillExt = JSON.parseObject(JSON.toJSONString(this), JxcStockInbillExt.class);
        List<JxcStockInbillItemExt> detailExtList = inbillExt.getItem_list();
        if (JxcSettingUtil.isClothing()) {
            detailExtList = new ArrayList<>();
            int sn = 1;
            for (JxcStockInBillItemReqParamsVo paramsVo : this.getItem_list()) {
                for (Map.Entry<String, BigDecimal> item : paramsVo.getPreset_qty_map().entrySet()) {
                    JxcStockInbillItemExt itemExt = JSON.parseObject(JSON.toJSONString(paramsVo), JxcStockInbillItemExt.class);
                    Map<String, String> idMap = paramsVo.getPreset_size_id_map();
                    if (idMap != null && idMap.containsKey(item.getKey())) {
                        String[] strings = idMap.get(item.getKey()).split("_");
                        if (strings.length == 2) {
                            itemExt.setId(Long.valueOf(strings[0]));
                            itemExt.setSid(strings[1]);
                        }
                    }
                    Map<String, String> productMap = paramsVo.getPreset_product_map();
                    if (productMap != null && productMap.containsKey(item.getKey())) {
                    }
//                    Map<String, List<JxcStockInbillItemPositionExt>> positionMap = paramsVo.getPreset_position_map();
//                    List<JxcStockInbillItemPositionExt> positionExtList = new ArrayList<>();
//                    if (positionMap != null && positionMap.containsKey(item.getKey())) {
//                        List<JxcStockInbillItemPositionExt> extList = positionMap.get(item.getKey());
//                        for (JxcStockInbillItemPositionExt positionExt : positionExtList) {
//                            JxcStockInbillItemPositionExt ext = ListUtil.get(extList, t -> positionExt.getPosition_sid().equals(t.getPosition_sid()));
//                            if (ext != null) {
//                                positionExt.setId(ext.getId());
//                                positionExt.setSid(ext.getSid());
//                                positionExt.setInbill_sid(ext.getInbill_sid());
//                                positionExt.setInbill_item_sid(ext.getInbill_item_sid());
//                            }
//                        }
//                    }
//                    itemExt.setIn_qty(item.getValue());
//                    itemExt.setSn(sn);
//                    itemExt.setPosition_list(positionExtList);
                    detailExtList.add(itemExt);
                    sn += 1;
                }
            }
        }
        inbillExt.setItem_list(detailExtList);
        return inbillExt;
    }

    public static JxcStockInBillReqParamsVo transfer(JxcStockInbillExt inbillExt, JxcSettingVo settingVo) {
        if (inbillExt == null) {
            return null;
        }
        JxcStockInBillReqParamsVo paramsVo = JSON.parseObject(JSON.toJSONString(inbillExt), JxcStockInBillReqParamsVo.class);
        List<JxcStockInbillItemExt> itemExtList = inbillExt.getItem_list();
        List<JxcStockInBillItemReqParamsVo> voList = new ArrayList<>();
        if (!HlpUtils.isEmptyList(itemExtList)) {
            List<String> sizeList = ClothingUtil.getClothing_preset_size_list(settingVo);
            JxcGoodsMapper jxcGoodsMapper = SpringContextUtil.getBean(JxcGoodsMapper.class);
            Map<String, Object> params = new HashMap<>();
            params.put("goods_list", ListUtil.map(itemExtList, JxcStockInbillItemExt::getGoods_sid));
            List<JxcGoodsVo> goodsVoList = jxcGoodsMapper.select4ClothingByPage(params, null);
            for (JxcGoodsVo goodsVo : goodsVoList) {
                List<JxcStockInbillItemExt> filterDetailList = null;//ListUtil.filter(itemExtList, t -> goodsVo.containsProduct(t.getProduct_sid()));
                if (HlpUtils.isEmptyList(filterDetailList)) {
                    continue;
                }
                JxcStockInBillItemReqParamsVo detailReqParamsVo = JSON.parseObject(JSON.toJSONString(filterDetailList.get(0)), JxcStockInBillItemReqParamsVo.class);
                AutoInjectUtils.clearBaseData(detailReqParamsVo);
                detailReqParamsVo.setSn(filterDetailList.get(0).getSn());
//                detailReqParamsVo.setPreset_product_map(goodsVo.getClothing_preset_product_map());
                Map<String, String> presetProductMap = null;//goodsVo.getClothing_preset_product_map();
                Map<String, BigDecimal> presetQtyMap = new HashMap<>();
//                Map<String, List<JxcStockInbillItemPositionExt>> presetPositionMap = new HashMap<>();
                Map<String, String> presetSizeIdMap = new HashMap<>();
                int index = 0;
                for (String size : sizeList) {
                    String productSid = presetProductMap.get(size);
                    if (HlpUtils.isEmpty(productSid)) {
                        index += 1;
                        continue;
                    }
                    JxcStockInbillItemExt detailExt = null;//ListUtil.get(filterDetailList, t -> productSid.equals(t.getProduct_sid()));
                    if (detailExt == null) {
                        index += 1;
                        continue;
                    }
                    presetQtyMap.put("preset_qty_" + index, detailExt.getIn_qty());
//                    presetPositionMap.put(size, detailExt.getPosition_list());
                    presetSizeIdMap.put(size, String.format("%s_%s", detailExt.getId(), detailExt.getSid()));
                    index += 1;
                }
                detailReqParamsVo.setPreset_qty_map(presetQtyMap);
//                detailReqParamsVo.setPreset_position_map(presetPositionMap);
//                detailReqParamsVo.setPreset_size_id_map(presetSizeIdMap);
//                double totalQty = 0D, totalAmount = 0D, totalPositionInQty = 0D;
//                List<JxcStockInbillItemPositionExt> positionExtList = new ArrayList<>();
//                for (JxcStockInbillItemExt itemExt : filterDetailList) {
//                    totalQty = DoubleUtil.add(totalQty, itemExt.getIn_qty());
//                    totalAmount = DoubleUtil.add(totalAmount, itemExt.getAmount());
//                    totalPositionInQty = DoubleUtil.add(totalPositionInQty, itemExt.getPosition_in_qty());
//                    positionExtList.addAll(itemExt.getPosition_list());
//                }
//                detailReqParamsVo.setIn_qty(BigDecimal.valueOf(totalQty));
//                detailReqParamsVo.setAmount(BigDecimal.valueOf(totalAmount));
                voList.add(detailReqParamsVo);
            }
        }
        voList = ListUtil.sorted(voList, Comparator.comparing(JxcStockInBillItemReqParamsVo::getSn));
        paramsVo.setItem_list(voList);
        return paramsVo;
    }
}
